<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="名称">
          <el-input
            v-model="queryParams.keywords"
            placeholder="输入查询关键字"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >

          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :stripe="true"
        size="large"
      >
        <el-table-column align="center" label="序号" type="index" width="100" />
        <el-table-column align="center" label="实验室名称" prop="labName" />
        <el-table-column align="center" label="座位id" prop="seatNo" />
        <el-table-column align="center" label="申请人" prop="bookUser" />
        <el-table-column align="center" label="开始时间" prop="bookStartTime" />
        <el-table-column align="center" label="结束时间" prop="bookEndTime" />
        <el-table-column align="center" fixed="right" label="操作" width="180">
          <template #default="scope">
            <el-popconfirm
              title="你确定要驳回?"
              @confirm="openDialog(scope.row.id)"
            >
              <template #reference>
                <el-button type="danger" text size="small">
                  <i-ep-delete />驳回
                </el-button>
              </template>
            </el-popconfirm>
            <el-button
              type="success"
              text
              size="small"
              @click.stop="pass(scope.row.id)"
              ><i-ep-edit />通过
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 预定表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="30%"
      @close="closeDialog"
    >
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="驳回原因" prop="reason">
          <el-input
            v-model="formData.reason"
            type="textarea"
            placeholder="请入驳回原因"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="overrule">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {
  getPageBookingApplicationAPI,
  overruleAPI,
  passAPI,
} from "@/api/book/index";

import { IPage, IBooking } from "@/api/book/types";

import { ref, reactive } from "vue";

const total = ref(0);
const loading = ref(false);
const tableData = ref<IBooking[]>([]);
const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);
const labList = ref<any>([]);
const seatList = ref<any[]>([]);
// 表单数据
const formData = reactive<IBooking>({});
const openTime = ref<any>();
const cascader = ref<any>();

// 表单规则
const rules = reactive({
  reason: [{ required: true, message: "请输入", trigger: "blur" }],
});

const dialog = reactive<DialogOption>({
  visible: false,
});
const queryParams = reactive<IPage>({
  pageNum: 1,
  pageSize: 10,
});

onMounted(() => {
  handleQuery();
});

// 获取预定列表
function handleQuery() {
  loading.value = true;
  getPageBookingApplicationAPI(queryParams)
    .then(({ data }) => {
      tableData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */

function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 打开预定表单弹窗 */
function openDialog(id?: number) {
  formData.id = id;
  dialog.title = "驳回";
  dialog.visible = true;
}

/** 驳回 */
function overrule() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      overruleAPI(formData);
      ElMessage.success("操作成功");
      closeDialog();
      resetQuery();
    }
  });
}

/** 重置表单 */
function resetForm() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
  cascader.value = undefined;
  openTime.value = undefined;
}
/** 删预定 */
function pass(id: number) {
  loading.value = true;
  passAPI(id)
    .then(() => {
      ElMessage.success("操作成功");
      resetQuery();
    })
    .finally(() => (loading.value = false));
  resetForm();
}
</script>

<style lang="scss" scoped></style>
